package com.fish.web.passport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.order.po.Order;
import com.fish.passport.po.PassCode;
import com.fish.passport.po.PassEvent;
import com.fish.passport.po.PassPortCode;
import com.fish.service.BusinessLogicService;
import com.fish.service.order.OrderService;
import com.fish.service.passport.MakePassCodeService;
import com.fish.service.passport.PassCodeService;

public class PassPortAction extends BaseAction {
	private static final long serialVersionUID = -2807552421422975297L;
	private PassCodeService passCodeService;
	private MakePassCodeService makePassCodeService;
	private BusinessLogicService businessLogicService;
	private OrderService orderService;
	private PassCode passCode = new PassCode();
	private List<PassCode> passCodeList = new ArrayList<PassCode>();
	private List<PassPortCode> passPortList;
	private String startDate;
	private String endDate;
	private QueryPage queryPage = new QueryPage();
	public String goToPassPort(){
		 return SUCCESS;
	}
	public String passCodeList(){
		if(startDate!=null&&!"".equals(startDate)&&endDate!=null&&!"".equals(endDate)){			
				passCode.setStartDate(convertToDate(startDate));
				passCode.setEndDate(convertToDate(endDate));						
		}
		int count = passCodeService.selectRowCount(passCode);
		if(count > 0){
			queryPage = new QueryPage(count, 10, queryPage.getCurPage());
		}		
		passCode.setBeginItem(queryPage.getBeginItem());
		passCode.setEndItem(queryPage.getPageSize());
		this.passCodeList=this.passCodeService.queryPassCodes(passCode);
		return SUCCESS;		
	}
	
	public String onApplay (){
		this.passCode=this.makePassCodeService.getPassCodeByCodeId(this.passCode.getCodeId());
		boolean applaySuccess=this.passCodeService.onApply(this.passCode.getCodeId());
		if(applaySuccess){
			Order order=orderService.queryOrderByPrimaryKey(this.passCode.getOrderId());
			businessLogicService.applyCodeSuccessLogic(order);
		}
		return passCodeList();
	}
	/**
	 * 重新申请码
	 * @return
	 */
	public String reApplay (){
		String newCodeId=makePassCodeService.reApplyCode(passCode.getCodeId());
		this.passCodeService.onApply(newCodeId);
		return passCodeList();
	}
	/**
	 * 重发短信
	 * @return
	 */
	public String reSendSms (){
		PassEvent event = makePassCodeService.resend(passCode.getCodeId());
		this.passCodeService.onEvent(event.getEventId());
		return passCodeList();
	}
	/**
	 * 废码
	 * @return
	 */
	public String destroy (){
		PassEvent event = makePassCodeService.destroyCode(passCode.getCodeId());
		this.passCodeService.onEvent(event.getEventId());
		return passCodeList();
	}
	/**
	 * 查看通关信息
	 * @return
	 */
	public String viewPassPort (){
		this.passPortList = this.passCodeService.searchPassPortByCodeId(passCode.getCodeId());
		return SUCCESS;
	}
	
	private Date convertToDate(String dateStr){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			return format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String goToDeviceList(){
		 return SUCCESS;
	}
	public String goToProviderList(){
		 return SUCCESS;
	}
	public PassCodeService getPassCodeService() {
		return passCodeService;
	}
	public void setPassCodeService(PassCodeService passCodeService) {
		this.passCodeService = passCodeService;
	}
	public PassCode getPassCode() {
		return passCode;
	}
	public void setPassCode(PassCode passCode) {
		this.passCode = passCode;
	}
	public List<PassCode> getPassCodeList() {
		return passCodeList;
	}
	public void setPassCodeList(List<PassCode> passCodeList) {
		this.passCodeList = passCodeList;
	}
	public List<PassPortCode> getPassPortList() {
		return passPortList;
	}
	public void setPassPortList(List<PassPortCode> passPortList) {
		this.passPortList = passPortList;
	}
	public void setMakePassCodeService(MakePassCodeService makePassCodeService) {
		this.makePassCodeService = makePassCodeService;
	}
	public void setBusinessLogicService(BusinessLogicService businessLogicService) {
		this.businessLogicService = businessLogicService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public QueryPage getQueryPage() {
		return queryPage;
	}
	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}
	
}
